package cn.xz.mytimer;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;


/**
 * 网络请求工具类
 * */
public class OkHttpUtil {

    //Get请求无需返回结果
    public static void toGet(String s){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(s).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //ToastUtil.to(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    //请求成功
                }else {
                    //ToastUtil.to("请求失败");
                }
            }
        });
    }

    //Get请求有返回结果
    public static void toGet(String s,HttpCallBack httpCallBak){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(s).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //ToastUtil.to(e.getMessage());
                httpCallBak.error();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    //请求成功
                    httpCallBak.success();
                }else {
                    //ToastUtil.to("请求失败");
                    httpCallBak.error();
                }
            }
        });
    }

    //Get请求有返回参数
    public static <T> void toGet(String s,RequestSuccess onSuccess, final Class<T> reponseClass){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(s).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                onSuccess.error();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String responseString = response.body().string();
                    if (responseString != null){
                        Response responseTemp = response.newBuilder().body(ResponseBody.create(response.body().contentType(), responseString)).build();
                        T gsonResponse = new Gson().fromJson(responseTemp.body().string(), reponseClass);
                        onSuccess.success(gsonResponse);
                    }else {
                        onSuccess.error();
                    }
                }else {
                    onSuccess.error();
                }
            }
        });
    }



    //Post请求
    public void toPost(String s){
        OkHttpClient  okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("name","zhangqilu")
                .add("age","25")
                .build();
        Request request = new Request.Builder().url(s).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    RequestSuccess.success(response.body().string());
                }else {
                }
            }
        });
    }

    public static HttpCallBack httpCallBak;
    public interface HttpCallBack{
        void success();
        void error();
    }



    //这里创建一个回调，请求成功时调用
    public static RequestSuccess RequestSuccess;
    public interface RequestSuccess<T>{
        void success(T data);
        void error();
    }
    public static void setRequestSuccess(OkHttpUtil.RequestSuccess requestSuccess) {
        RequestSuccess = requestSuccess;
    }
}
